﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PMisuseAn.behaviour.model;
using PMisuseAn.io;

namespace PMisuseAn
{
    /// <summary>
    /// Interaction logic for AddAgentWindow.xaml
    /// </summary>
    public partial class AddAgentWindow : Window
    {
        Group group_;
        Agent agent_;
        List<string> agentNames;
        List<string> machines;
        List<string> actors;

        public AddAgentWindow(Group group)
        {
            InitializeComponent();
            group_ = group;

            loadAllAgents_cb();
            loadAllMachines_cb();
            loadAllAgentTypes_cb();
        }

        public AddAgentWindow(Agent agent)
        {
            InitializeComponent();
            loadAllMachines_cb();
            loadAllAgentTypes_cb();

            agent_ = agent;
            agent_names_cb.Text = agent.agentName;
            agent_names_cb.IsEnabled = false;

            state_machines_cb.SelectedItem = agent.assignedMachine.machineName;
            state_machines_cb.IsEnabled = false;
            
            actors_cb.SelectedItem = agent.actorType.name;
            actors_cb.IsEnabled = false;

            add_agent_b.IsEnabled = false;

            loadAgentCurrentGroups();
        }

        public void loadAllAgents_cb()
        {
            agentNames = MisUseAnObjectsDB.loadAllAgentNames(MainWindow.clusterName);
            agent_names_cb.ItemsSource = agentNames;
        }

        public void loadAllMachines_cb()
        {
            machines = MisUseAnObjectsDB.loadAllmachines(MainWindow.clusterName);
            state_machines_cb.ItemsSource = machines;
        }

        public void loadAllAgentTypes_cb()
        {
            actors = MisUseAnObjectsDB.loadActorNames(MainWindow.clusterName);
            actors_cb.ItemsSource = actors;
        }

        public void loadAgentCurrentGroups()
        {
            current_groups_lb.Items.Clear();
            foreach (string s in agent_.associatedGroups)
            {
                ListBoxItem l = new ListBoxItem();
                l.Content = s;
                current_groups_lb.Items.Add(l);
            }
        }

        private void agentName_cb_selection_changed(object sender, SelectionChangedEventArgs e)
        {
            string agentName = null;
            if (agent_names_cb.SelectedItem != null)
            {
                agentName = agent_names_cb.SelectedItem.ToString();
            }
            else
            {
                agentName = agent_names_cb.Text;
            }

            agentName = agentName.Trim();
            agentName = agentName.TrimEnd('\n');
            agentName = agentName.TrimEnd('\r');

            if (agentName != null && agentName.Count() > 0)
            {
                bool exist_group = false;
                foreach (string gs in GroupConfigWindow.groupNames)
                {
                    if(gs.Equals(group_.groupName))
                    {
                        exist_group = true;
                        break;
                    }
                }

                if (exist_group == false)
                {
                    List<string> ass_gs = MisUseAnObjectsDB.loadAgentAssociatedGroups(agentName, MainWindow.clusterName);
                    foreach (string ass_g in ass_gs)
                    {
                        agent_ = MisUseAnObjectsDB.loadGroupAgentDB(agentName, ass_g, MainWindow.clusterName);
                        
                        if (agent_ != null)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    agent_ = MisUseAnObjectsDB.loadGroupAgentDB(agentName, group_.groupName, MainWindow.clusterName);
                }
            }
            if (agent_ != null)
            {
                loadAgentCurrentGroups();
                state_machines_cb.SelectedItem = agent_.assignedMachine.machineName;
                state_machines_cb.IsEnabled = false;
                actors_cb.SelectedItem = agent_.actorType.name;
                actors_cb.IsEnabled = false;
            }
            else
            {
                agent_ = null;
                current_groups_lb.Items.Clear();
                actors_cb.IsEnabled = true;
                state_machines_cb.IsEnabled = true;
            }
        }

        private void key_pressed_agentName_cb(object sender, KeyEventArgs e)
        {
            //working but suspended
            //string agentName = null;
            //if (agent_names_cb.SelectedItem != null)
            //{
            //    agentName = agent_names_cb.SelectedItem.ToString();
            //}
            //else
            //{
            //    agentName = agent_names_cb.Text;
            //}

            //agentName = agentName.Trim();
            //agentName = agentName.TrimEnd('\n');
            //agentName = agentName.TrimEnd('\r');

            //if (agentName != null && agentName.Count() > 0)
            //{
            //    agent_ = MisUseAnObjectsDB.loadGroupAgentDB(agentName, group_.groupName, MainWindow.clusterName);
            //}
            //if (agent_ != null)
            //{
            //    loadAgentCurrentGroups();
            //    state_machines_cb.IsEnabled = false;
            //    actors_cb.SelectedItem = agent_.actorType.name;
            //    actors_cb.IsEnabled = false;
            //}
            //else
            //{
            agent_ = null;
            current_groups_lb.Items.Clear();
            actors_cb.IsEnabled = true;
            state_machines_cb.IsEnabled = true;
            //}
        }

        private void add_agent_b_Click(object sender, RoutedEventArgs e)
        {
            if (agent_ == null)
            {
                string agentName = null;
                agentName = agent_names_cb.Text;

                if (agentName == null || agentName.Equals(""))
                {
                    agentName = agent_names_cb.SelectedItem.ToString();     
                }

                agentName = agentName.Trim();
                agentName = agentName.TrimEnd('\n');
                agentName = agentName.TrimEnd('\r');

                foreach (string a_s in agentNames)
                {
                    if (agentName.Equals(a_s))
                    {
                        MessageBox.Show(agentName+" already exist.", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                        return;
                    }
                }

                string machineName = null;
                if (state_machines_cb.SelectedItem != null)
                {
                    machineName = state_machines_cb.SelectedItem.ToString();
                }
                else
                {
                    machineName = state_machines_cb.Text;
                }

                string actorName = null;
                if (actors_cb.SelectedItem != null)
                {
                    actorName = actors_cb.SelectedItem.ToString();
                }
                else
                {
                    actorName = actors_cb.Text;
                }

                if ((actorName == null || actorName.Equals(""))
                    || ((machineName == null) || machineName.Equals(""))
                    || ((agentName == null) || agentName.Equals("")))
                {
                    MessageBox.Show("Missing parameter", "Error", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }

                else
                {
                    agent_ = new Agent();
                    agent_.clusterName = MainWindow.clusterName;
                    agent_.agentName = agentName;
                    StateMachine machine = MisUseAnObjectsDB.loadStateMachineDB(machineName, MainWindow.clusterName);
                    Actor actor = MisUseAnObjectsDB.loadActor(actorName, MainWindow.clusterName);
                    agent_.actorType = actor;
                    agent_.assignedMachine = machine;
                }

            }
            foreach (Agent a in group_.members)
            {
                if (a.agentName.Equals(agent_.agentName))
                {
                    MessageBox.Show(a.agentName+" is already a member of selected group..", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                    return;
                }
            }

            group_.addMember(agent_);

            this.Close();
        }
    }
}
